package com.itcam.servlet_.loginmodel.control;

import com.itcam.servlet_.loginmodel.entity.User;
import com.itcam.servlet_.loginmodel.service.UserService;
import com.itcam.servlet_.loginmodel.service.impl.UserServiceImpl;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;


import java.io.IOException;

/**
 * Author:Cammy.Wu
 * Date:2024-06-27
 * Description:
 */

public class LoginServlet extends HttpServlet {

    UserService userService = new UserServiceImpl();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        UserService userService = new UserServiceImpl();

        // 获取请求数据
        String username = req.getParameter("username");
        String password = req.getParameter("password");

        // 封装对象
        User user = new User(username, password);
        // 调用service进行逻辑处理
        User u = userService.checkUser(user);
        System.out.println("u = " + u);

        if (u != null) {
            // 登录成功
            System.out.println("successfully");
        } else {
            // 登录失败
            System.out.println("failed");
        }


    }
}
